package com.example.controller;

import jakarta.annotation.Resource;
import org.springframework.ai.chat.Generation;
import org.springframework.ai.chat.messages.Message;
import org.springframework.ai.chat.messages.UserMessage;
import org.springframework.ai.chat.prompt.Prompt;
import org.springframework.ai.ollama.OllamaChatClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@RestController
@RequestMapping("/demo1")
public class DemoController {

    @Resource
    private OllamaChatClient chatClient;

    @GetMapping("/chat")
    public String ollamaChatClient() {
        String content = "内容";

        Message tipsMessage = new UserMessage("提示词");

        List<Message> userMessages = Stream.of(tipsMessage, new UserMessage(content)).collect(Collectors.toList());
        Prompt prompt = new Prompt(userMessages);
        Generation generation = chatClient.call(prompt).getResult();
        String result = generation != null ? generation.getOutput().getContent() : "";
        System.out.println(result);
        return result;
    }
}
